package com.bjc.platform.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.bjc.platform.dao.GenericDao;
import com.bjc.platform.model.AdmMenu;
import com.bjc.platform.service.AdmMenuService;
import com.bjc.platform.vo.ExtTreeModel;

@Service
public class AdmMenuServiceImpl implements AdmMenuService {

	@Autowired
	private GenericDao genericDao;

	@Override
	public List<Map<String, Object>> getMenuTree(String roleId) {
		return genericDao.queryBySqlToMap(sqlBuf.toString(),
				new Object[] { roleId });
	}

	@Override
	public List<Map<String, Object>> getMenuTreeByUserId(Long userId) {
		return genericDao.queryBySqlToMap(sqlMenuByRoleId.toString(),
				new Object[] { userId });
	}

	// 查询权限结构树的sql语句
	private static StringBuilder sqlBuf = new StringBuilder();
	static {
		sqlBuf.append("	SELECT");
		sqlBuf.append("	t4.id ").append(ExtTreeModel.ID);
		sqlBuf.append("	,t4.`code` ");
		sqlBuf.append("	,t4.`name` ").append(ExtTreeModel.NAME);
		sqlBuf.append("	,t4.parent_id ").append(ExtTreeModel.PARENT_ID);
		sqlBuf.append("	,t3.checked ").append(ExtTreeModel.CHECKED);
		sqlBuf.append("	,CASE");
		sqlBuf.append("		WHEN t5.id IS NOT NULL THEN");
		sqlBuf.append("			'hasChildren'");
		sqlBuf.append("		ELSE");
		sqlBuf.append("			NULL");
		sqlBuf.append("		END hasChildren");
		sqlBuf.append("	,t4.sort");
		sqlBuf.append("	,case when t4.parent_id is null then t4.id");
		sqlBuf.append("		else t4.parent_id end order_parent");
		sqlBuf.append(" FROM");
		sqlBuf.append("	(");
		sqlBuf.append("		SELECT");
		sqlBuf.append("			t.id,");
		sqlBuf.append("			CASE");
		sqlBuf.append("			WHEN t2.role_id IS NOT NULL THEN");
		sqlBuf.append("				'checked'");
		sqlBuf.append("			ELSE");
		sqlBuf.append("				NULL");
		sqlBuf.append("			END checked");
		sqlBuf.append("		FROM");
		sqlBuf.append("			adm_resource t");
		sqlBuf.append("		LEFT JOIN adm_authority t2 ON t2.res_id = t.id  and t2.role_id = ? ");
		sqlBuf.append("		) t3");
		sqlBuf.append("	LEFT JOIN adm_menu t4 ON t3.id = t4.id");
		sqlBuf.append("	LEFT JOIN adm_menu t5 ON t5.id = t4.id");
		sqlBuf.append("	AND EXISTS (");
		sqlBuf.append("		SELECT");
		sqlBuf.append("			*");
		sqlBuf.append("		FROM");
		sqlBuf.append("			adm_menu t6");
		sqlBuf.append("		WHERE");
		sqlBuf.append("			t6.parent_id = t5.id");
		sqlBuf.append("	)  where t4.id is not null ");
		sqlBuf.append("order by order_parent, t4.sort, t4.id");
	}

	// 查询指定角色ID的菜单资源
	private static StringBuilder sqlMenuByRoleId = new StringBuilder();
	static {
		sqlMenuByRoleId.append("	SELECT");
		sqlMenuByRoleId.append("	t4.id ").append(ExtTreeModel.ID);
		sqlMenuByRoleId.append("	,t4.`code` ").append(ExtTreeModel.CODE);
		sqlMenuByRoleId.append("	,t4.icon ").append(ExtTreeModel.ICON);
		sqlMenuByRoleId.append("	,t4.`name` ").append(ExtTreeModel.NAME);
		sqlMenuByRoleId.append("	,t4.parent_id ")
				.append(ExtTreeModel.PARENT_ID);
		sqlMenuByRoleId.append("	,CASE");
		sqlMenuByRoleId.append("		WHEN t5.id IS NOT NULL THEN");
		sqlMenuByRoleId.append("			'hasChildren'");
		sqlMenuByRoleId.append("		ELSE");
		sqlMenuByRoleId.append("			NULL");
		sqlMenuByRoleId.append("		END ").append(ExtTreeModel.HAS_CHILDREN);
		sqlMenuByRoleId.append("	,t4.sort");
		sqlMenuByRoleId.append("	,case when t4.parent_id is null then t4.id");
		sqlMenuByRoleId.append("		else t4.parent_id end order_parent");
		sqlMenuByRoleId.append(" FROM");
		sqlMenuByRoleId.append("	(");
		sqlMenuByRoleId.append("		SELECT");
		sqlMenuByRoleId.append("		DISTINCT	t.id");
		sqlMenuByRoleId.append("		FROM");
		sqlMenuByRoleId.append("			adm_resource t");
		sqlMenuByRoleId
				.append("		INNER JOIN adm_authority t2 ON t2.res_id = t.id  and t2.role_id in ( ");
		sqlMenuByRoleId
				.append("		 select role_id from adm_user_role it where user_id = ? ");
		sqlMenuByRoleId.append("		)) t3");
		sqlMenuByRoleId.append("	LEFT JOIN adm_menu t4 ON t3.id = t4.id");
		sqlMenuByRoleId.append("	LEFT JOIN adm_menu t5 ON t5.id = t4.id");
		sqlMenuByRoleId.append("	AND EXISTS (");
		sqlMenuByRoleId.append("		SELECT");
		sqlMenuByRoleId.append("			*");
		sqlMenuByRoleId.append("		FROM");
		sqlMenuByRoleId.append("			adm_menu t6");
		sqlMenuByRoleId.append("		WHERE");
		sqlMenuByRoleId.append("			t6.parent_id = t5.id");
		sqlMenuByRoleId.append("	) where t4.id is not null ");
		sqlMenuByRoleId.append("order by order_parent, t4.sort, t4.id");
	}

	@Override
	public List<AdmMenu> getParentMenu(String[] menuIds) {
		StringBuilder ph = new StringBuilder();
		List<String> params = new ArrayList<String>();
		if (menuIds != null && menuIds.length > 0) {
			for (int i = 0; i < menuIds.length; i++) {
				if (i > 0) {
					ph.append(",");
				}
				ph.append("?");
				params.add(menuIds[i]);
			}

			params.addAll(params);

			StringBuilder sb = new StringBuilder();
			sb.append("select distinct m from AdmMenu m where m.id in (");
			sb.append(" select parentId from AdmMenu where id in (");
			sb.append(ph.toString());
			sb.append(")");
			sb.append(" or parentId in (");
			sb.append(ph.toString());
			sb.append("))");

			return (List<AdmMenu>) genericDao.queryByJpql(sb.toString(),
					params.toArray(), null);
		} else {
			return null;
		}
	}
}
